package com.hn.gege.sys.conf.security;


import com.hn.gege.sys.modules.sys.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户详情
 */
public class AdminUserDetails implements UserDetails {
    private User umsAdmin;
    private String roleCode;
    private List<String> juris;

    public AdminUserDetails(User umsAdmin, String roleCode, List<String> juris) {
        this.umsAdmin = umsAdmin;
        this.roleCode = roleCode;
        this.juris = juris;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return juris.stream()
                .filter(item -> item != null)
                .map(item ->new SimpleGrantedAuthority(item))
                .collect(Collectors.toList());
    }


    @Override
    public String getPassword() {
        return umsAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return umsAdmin.getPhone();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


    public boolean isEnabled() {
        return "1".equals(umsAdmin.getState()) ;
    }

    public User getUser(){
        return this.umsAdmin;
    }
    public String getRoleCode(){
        return this.roleCode;
    }
}
